package com.baizhi.service.impl;

import com.baizhi.dao.AdminDAO;
import com.baizhi.entity.Admin;
import com.baizhi.entity.Resource;
import com.baizhi.entity.Role;
import com.baizhi.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDAO adminDAO;
    @Override
    public Admin login(String username, String password, HttpSession session) {
        Admin admin = adminDAO.selectOne(username);
        if(admin!=null&&!password.equals("")){
            if(admin.getPassword().equals(password)){
                session.setAttribute("admin",admin);
                return admin;
            }
        }
        return null;
    }

    @Override
    public Boolean insert(Admin admin) {
        Boolean a = null;
        try{
            adminDAO.insert(admin);
            a = true;
        }catch(Exception e){
            a = false;
        }
        return a;
    }

    @Override
    public Boolean update(Admin admin) {
        Boolean a = null;
        try{
            System.out.println("admin = " + admin);
            adminDAO.update(admin);
            a = true;
        }catch(Exception e){
            a = false;
            e.printStackTrace();
        }
        return a;
    }

    @Override
    public PageInfo<Admin> selectAll(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Admin> admins = adminDAO.selectAll();
        PageInfo<Admin> pageInfo = new PageInfo<>(admins);
        return pageInfo;
    }

    @Override
    public Admin selectOne(String username) {
        Admin admin = adminDAO.selectOne(username);
        return admin;
    }

    @Override
    public Boolean delete(Integer[] ids) {
        Boolean a = null;
        try{
            adminDAO.delete(ids);
            a = true;
        }catch(Exception e){
            a = false;
            e.printStackTrace();
        }
        return a;
    }

    @Override
    public Set<String> selectOneRole(String username) {
        Admin admin = adminDAO.selectOneRole(username);
        List<Role> roles = admin.getRoles();
        Set<String> set = new HashSet<>();
        for (Role role : roles) {
            set.add(role.getRoleName());
        }
        System.out.println("admin = " + admin);
        return set;
    }

    @Override
    public Set<String> selectResource(String username) {
        Admin admin = adminDAO.selectResource(username);
        Set<String> set = new HashSet<>();
        List<Role> roles = admin.getRoles();
        for (Role role : roles) {
            List<Resource> resources = role.getResources();
            for (Resource resource : resources) {
                set.add(resource.getResourcePermission());
            }
        }
        return set;
    }
}
